<?php
class CommonAction extends BaseAction {
    public $group, $ac, $mod;
    public $mid,$member;
    public function _initialize() {
      parent::_initialize();
    	$this->ac = ACTION_NAME;
   		$this->mod = $this->getActionName();
   		$this->group = GROUP_NAME;
   		$this->assign('ac',$this->ac);
      
   		$this->assign('left_mod',$this->mod);


      $this->mid = session('apps_mid');
      	if ( !$this->mid ) 
      		redirect(U('Member/Public/login'),3,'请先登录，正在跳转到登陆页面');
  		$Member = D('Member');
  		$this->member = $Member->find($this->mid);
  		$this->assign('member',$this->member);
  		$this->assign('mid',$this->mid);

      $Plus = M('Plus');
      unset($map);
      $map['mid'] = $this->mid;
      $myPlusList = $Plus->where($map)->select();
      $this->assign( 'myPlusList',$myPlusList );

      if ( strtolower( $this->ac ) == "index" ) {
        $this->assign( 'do', U( $this->mod.'/add' ) );
        $this->assign( 'do_search', U( $this->mod.'/search' ) );
      }elseif ( strtolower( $this->ac ) == "add" ) {
        $this->assign( 'do', U( $this->mod.'/insert' ) );
      }elseif ( strtolower( $this->ac ) == "edit" ) {
        $this->assign( 'do', U( $this->mod.'/update' ) );
      }
    }


    protected function _search( $name = '' ) {
      //生成查询条件
      if ( empty( $name ) ) {
        $name = $this->actionName;
      }
      // $name = $this->getActionName();
      $model = D( $name );
      $map = array();
      foreach ( $model->getDbFields() as $key => $val ) {
        if ( isset( $_REQUEST [$val] ) && $_REQUEST [$val] != '' ) {
          $map [$val] = $_REQUEST [$val];
        }
      }
      return $map;
    }

    public function index() {
      //列表过滤器，生成查询Map对象
      $map = $this->_search();
      if ( method_exists( $this, '_filter' ) ) {
        $map = $this->_filter();
      }
      $map['mid'] = $this->mid;
      $model = D( $this->mod );
      if ( !empty( $model ) ) {
        $this->_list( $model, $map );
      }
      cookie( '_currentUrl_', __SELF__ );
      $this->display();
      return;
    }

    public function add()
    {
      $this->display();
    }

    public function insert()
    {
        $_POST['mid'] = $this->mid;
        $model = D( $this->mod );
        if ( false === $model->create() ) {
          $this->error( $model->getError() );
        }
        //保存当前数据对象
        $list = $model->add();
        if ( $list !== false ) { //保存成功
          $this->success( '新增成功!', cookie( '_currentUrl_' ) );
        } else {
          //失败提示
          $this->error( '新增失败!' );
        }
    }

  function edit() {
    $model = M( $this->mod );
    $id = I( $model->getPk() );
    $map['id'] = $id;
    $map['mid'] = $this->mid;
    $vo = $model->where( $map )->find();
    if ( !$vo ) {
      $this->error( "参数错误.非法参数." );
      exit();
    }
    $this->assign( 'vo', $vo );
    $this->display();
  }

  function update() {
    $model = D( $this->mod );
    if ( false === $model->create() ) {
      $this->error( $model->getError() );
    }
    $id = I( $model->getPk() );
    // 更新数据
    $map['mid'] = $this->mid;
    $map[$model->getPk()] = $id;
    $list = $model->where( $map )->save();
    if ( false !== $list ) {
      //成功提示
      $this->success( '编辑成功!', cookie( '_currentUrl_' ) );
    } else {
      //错误提示
      $this->error( '编辑失败!' );
    }
  }

  public function delete() {
    $model = D( $this->mod );
    if ( !empty( $model ) ) {
      $pk = $model->getPk();
      $id = I( $pk );
      if ( isset( $id ) ) {
        $map[$pk] = array( 'in', explode( ',', $id ) );
        $map['mid'] = $this->mid;
        $restult = $model->where( $map )->delete();
        // print_r($model->getlastSql());
        // exit();
        if ( false != $restult ) {
          $this->success( '删除成功！' );
        } else {
          $this->error( '删除失败！' );
        }
      } else {
        $this->error( '非法操作' );
      }
    }
  }

    function success($msg = "", $reUrl = ""){
      $this->assign('msg',$msg);
      if ($reUrl == "") {
        $reUrl = cookie( '_currentUrl_' );
      }
      $this->assign('reUrl',$reUrl);
      $this->display("Public:success");
      exit();
    }

    function error($msg = "", $reUrl = ""){
      if ($reUrl == "") {
        $reUrl = cookie( '_currentUrl_' );
      }
      $this->assign('msg',$msg);
      $this->assign('reUrl',$reUrl);
      $this->display("Public:error");
      exit();
    }

    public function _list($model, $map, $sortBy = ' id desc')
    {
      $page = I('p',1);
      if ($page < 1) $page = 1;
      $pger  = $model->_list_($map,$page,16,$sortBy);
      $this->assign('volist',$pger['volist']);
      $this->assign('pger',$pger);

      $pgpam = $map;
      $pgvo =  array();
      unset($pgpam['mid']);
      for ($i = 0 ; $i < $pger['pagecount']; $i++) { 
        $pg = array();
        $pgpam['p'] = $i+1;
        $urlParems = http_build_query($pgpam);
        $pg['url'] = U($this->group.'/'.$this->mod.'/'.$this->ac.'?'.$urlParems);
        if ($page == $i+1) {
          $pg['url'] = "#";
        }
        $pgvo[] = $pg;
      }
      $this->assign('pgvo',$pgvo);
      $this->assign('npage',$page + 1);
      $this->assign('ppage',$page - 1);
      return $pger['volist'];
    }

    
}